package com.navinfo.tripanalysis.common.arithmetic;

import com.navinfo.tripanalysis.common.arithmetic.annotation.Arithmetic;
import com.navinfo.tripanalysis.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.tripanalysis.common.util.ArithmeticUtils;
import com.navinfo.tripanalysis.common.arithmetic.common.CommonData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;
import com.navinfo.tripanalysis.common.arithmetic.custom.VarBrakeNormal;
import com.navinfo.tripanalysis.common.arithmetic.enums.EventStatus;
import com.navinfo.tripanalysis.common.config.ConfigMap;

import java.math.BigDecimal;

/**
 * 普通刹车算法<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 输出：统计<br/>
 */
@Arithmetic(name = "普通刹车算法")
public class BrakeNormal extends AbstractArithmetic<VarBrakeNormal> {
    /**
     * 普通刹车最小速度阀值
     */
    private int speedThreshold0 = ConfigMap.getInteger("NormalBrake_speed_min");
    /**
     * 普通刹车最大速度阀值
     */
    private int speedThreshold1 = ConfigMap.getInteger("NormalBrake_speed_max");

    /**
     * 是否是普通刹车
     * @return
     */
    private boolean isNormalBrake() {
        CommonData data = getCommonData();
        return data.getBrake()==1 && data.getSpeedKmh()>speedThreshold0 && data.getSpeedKmh()<=speedThreshold1;
    }

    @Override
    public void calculate() {
        logger.debug("进入普通刹车统计算法.");
        VarBrakeNormal var = super.getObj();
        CommonData data = getCommonData();

        boolean preStatus = var.isPreState();
        boolean curStatus = isNormalBrake();
        logger.debug("普通刹车统计，状态：pre:{},current:{}", preStatus, curStatus);

        switch (getEventStatus(preStatus, curStatus)) {
            case START:
                logger.debug("普通刹车统计，事件开始.");
                var.setNumber(var.getNumber() + 1);
                break;
            case IN:
                logger.debug("普通刹车统计，事件中.");
                var.setMileage(var.getMileage() +  data.getDffDistance());
                var.setTimeMs(var.getTimeMs() + data.getDffTime());
                break;
            case END:
                logger.debug("普通刹车统计，事件结束.");
                var.setMileage(var.getMileage() +  data.getDffDistance());
                var.setTimeMs(var.getTimeMs() + data.getDffTime());
                break;
            default:
                logger.debug("普通刹车统计，状态：上一次和本次都未普通刹车，跳出算法.");
                break;
        }

        //设置上一个点状态、供下个点计算
        var.setPreState(curStatus);
        logger.debug("普通刹车统计算法结束.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarBrakeNormal var = super.getObj();
        statisticDataOut.setCsbNumber(var.getNumber());
        statisticDataOut.setCsbMileage(ArithmeticUtils.mileageMToM(var.getMileage()));
        statisticDataOut.setCsbDuration(ArithmeticUtils.timeMsToSecond(var.getTimeMs()));
    }
}
